﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Server
{
    public partial class GUIWindow : Form
    {
        private Controller _controller;

        public GUIWindow(Controller controller)
        {
            _controller = controller;
            InitializeComponent();
            this.projectObserver.Controller = controller;
            this.gvProjects.DataSource = this._controller.GetProjects();
            this.gvUnits.DataSource = this._controller.GetComputingUnits();
            this.gvComputing.DataSource = this._controller.GetComputing();
        }

        private void gvProjects_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            System.Type type = ((DataGridView)sender).CurrentRow.DataBoundItem.GetType();
            this.projectObserver.SetProject((int)type.GetProperty("Id").GetValue(((DataGridView)sender).CurrentRow.DataBoundItem, null));
        }

        private void btnComputingRefresh_Click(object sender, EventArgs e)
        {
            this.gvComputing.DataSource = this._controller.GetComputing();
            this.gvComputing.Refresh();
        }

        private void btnProjectRefresh_Click(object sender, EventArgs e)
        {
            this.gvProjects.DataSource = this._controller.GetProjects();
            this.gvProjects.Refresh();
        }

        private void btnAddProject_Click(object sender, EventArgs e)
        {
            try
            {
                this._controller.AddProject(this.tbNewProjectName.Text);

                MessageBox.Show("Dodano nowy projekt",
                    "Uwaga",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information,
                    MessageBoxDefaultButton.Button1);
                this.btnProjectRefresh_Click(null, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Niemożna dodac nowego projektu z powodu : \n" + ex.Message,
                    "Uwaga",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation,
                    MessageBoxDefaultButton.Button1);

            }
        }

        private void timerAutoRefresh_Tick(object sender, EventArgs e)
        {
            this.gvUnits.DataSource = this._controller.GetComputingUnits();
            this.gvUnits.Refresh();
        }
    }
}
